package cn.anecansaitin.whimsy.client.gui.overlay;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.capabilities.IManaStorage;
import cn.anecansaitin.whimsy.common.register.ModItems;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.client.gui.overlay.ExtendedGui;
import net.neoforged.neoforge.client.gui.overlay.IGuiOverlay;
import org.jetbrains.annotations.Nullable;

public class ManaOverlay implements IGuiOverlay {
    private static final ResourceLocation GRAPH = new ResourceLocation(Whimsy.MODID, "textures/gui/overlay/mana.png");

    @Override
    public void render(ExtendedGui gui, GuiGraphics guiGraphics, float partialTick, int screenWidth, int screenHeight) {
        Minecraft mc = Minecraft.getInstance();
        LocalPlayer player = mc.player;
        ItemStack itemStack = player.getMainHandItem();

        if (!itemStack.is(ModItems.ROCK_SCEPTRE.get())) {
            return;
        }

        @Nullable IManaStorage storage = itemStack.getCapability(IManaStorage.MANA_STORAGE);

        if (storage == null) {
            return;
        }

        //顶部
        guiGraphics.blit(GRAPH, 0, 0, 0, 0, 41, 11);

        //根据实际的魔力数量添加槽位
        int i = 0;

//        for (int j = 0; j < 10; j++) {
//            //皮带
//            guiGraphics.blit(GRAPH, 16, 11 + i * 8, 16, 11, 26, 8);
//            //魔力颜色条
//            int c = 0x26e132;
//            float r = (float) (c >> 16 & 255) / 255.0F;
//            float g = (float) (c >> 8 & 255) / 255.0F;
//            float b = (float) (c & 255) / 255.0F;
//            guiGraphics.setColor(r, g, b, 1);
//            guiGraphics.blit(GRAPH, 13, 13 + i * 8, 13, 34, 32, 5);
//            guiGraphics.setColor(1, 1, 1, 1);
//            //瓶子
//            guiGraphics.blit(GRAPH, 10, 12 + i * 8, 10, 19, 38, 7);
//            //花纹
//            guiGraphics.blit(GRAPH, 15, 14 + i * 8, 15, 28, 28, 3);
//            i++;
//        }
        for (IManaStorage.ManaType manaType : storage.getAllMana()) {
            //皮带
            guiGraphics.blit(GRAPH, 16, 11 + i * 8, 16, 11, 26, 8);
            //魔力颜色条
            int color = manaType.color;
            int length = 32;
            int count = manaType.mana;
            int max = manaType.capacity;
            int left = Math.floorDiv((max - count) * length, max);
            float r = (float) (color >> 16 & 255) / 255.0F;
            float g = (float) (color >> 8 & 255) / 255.0F;
            float b = (float) (color & 255) / 255.0F;
            guiGraphics.setColor(r, g, b, 1);
            guiGraphics.blit(GRAPH, 13 + left, 13 + i * 8, 13 + left, 34, 32 - left, 5);
            guiGraphics.setColor(1, 1, 1, 1);
            //瓶子
            guiGraphics.blit(GRAPH, 10, 12 + i * 8, 10, 19, 38, 7);
            //花纹
            guiGraphics.blit(GRAPH, 15, 14 + i * 8, 15, 28, 28, 3);
            i++;
        }

        //底部
        guiGraphics.blit(GRAPH, 16, 11 + i * 8, 49, 0, 26, 15);
    }
}
